package imaa.escola.instrumento.repository;

import imaa.escola.instrumento.datatransfer.PesquisaInstrumentoDto;
import imaa.escola.instrumento.domain.Instrumento;
import imaa.framework.persistence.CustomRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

public class InstrumentoRepositoryImpl implements CustomRepository<Instrumento, PesquisaInstrumentoDto>{

	@Autowired
	private InstrumentoRepository repository;
	
	@Autowired
	private JdbcTemplate template;

	@SuppressWarnings("rawtypes")
	public List<Instrumento> findByExample(PesquisaInstrumentoDto dto) {
		String sql = "SELECT * FROM instrumento  WHERE 1=1 ";
		if (dto.nome != null) 
			sql += "AND LOWER(DSC_INSTRUMENTO) LIKE LOWER('%" + dto.nome + "%') ";
		if (dto.nivelComplexidade != null)
			sql += "AND NIVEL_COMPLEXIDADE = " + dto.nivelComplexidade + " ";
		
		
		List<Instrumento> lista = new ArrayList<Instrumento>();
		List<Map<String,Object>> rows = template.queryForList(sql);
		
		for (Map row : rows) {
			Instrumento instrumento = new Instrumento();
			instrumento.setId(Long.parseLong(String.valueOf(row.get("SEQ_INSTRUMENTO"))));
			instrumento.setNome((String)(row.get("DSC_INSTRUMENTO")));
			instrumento.setNivelComplexidade((String)row.get("NIVEL_COMPLEXIDADE"));
			
			
			lista.add(instrumento);
		}
		return lista;
	}
	

}
